import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../common/my_icon.dart';
import '../../components/exit_prompt/exit_prompt.dart';
import '../../components/loading/svg_loading.dart';
import '../../controller/notice_ctrl/notice_ctrl.dart';
import '../../router/router.dart';
import '../../utils/screenutil/fit.dart';
import 'components/alarm_list.dart';
import 'components/head.dart';
import 'components/info.dart';
import 'components/navs.dart';
import 'ctrl/home_ctrl.dart';

class Home extends StatelessWidget {
  // 渐变值
  LinearGradient lineargradient = LinearGradient(
    begin: Alignment.topCenter,
    end: Alignment.bottomCenter,
    stops: [0.1, 0.3, 0.5, 1.0],
    colors: [
      Color.fromRGBO(58, 98, 237, 1),
      Color.fromRGBO(251, 251, 251, 1),
      Color.fromRGBO(251, 251, 251, 1),
      Color.fromRGBO(251, 251, 251, 1)
    ],
  );

  // 导入控制器
  final homeCtrl = Get.put(HomeCtrl());

  @override //ExitPrompt
  Widget build(BuildContext context) {
    return Scaffold(
      body: ExitPrompt(
        child: Obx(
          () => homeCtrl.isInitComplete.value
              ? Container(
                  padding: EdgeInsets.only(top: statusBarHeight()),
                  decoration: BoxDecoration(
                    gradient: lineargradient,
                  ),
                  child: Container(
                    width: 640.f,
                    child: Stack(
                      children: [
                        SmartRefresher(
                          enablePullDown: true,
                          onRefresh: () => homeCtrl.onRefresh(),
                          controller: homeCtrl.refreshs.value,
                          header: WaterDropHeader(
                            refresh: Container(
                              padding: EdgeInsets.only(top: 10.f),
                              child: SVGLoading(
                                tips: '正在刷新请稍等',
                                type: SpinKitType.CubeGrid,
                                iconColor: Colors.white,
                                size: 20,
                                fontsColor: Colors.white,
                              ),
                            ),
                            complete: Container(
                              height: 60.f,
                              padding: EdgeInsets.only(top: 10.f),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Icon(
                                    MyIcon.success,
                                    color: Colors.white,
                                    size: 20.f,
                                  ),
                                  SizedBox(width: 10),
                                  Text(
                                    "更新成功",
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 24.f,
                                      fontFamily: 'RubikMonoOne',
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                          child: SingleChildScrollView(
                            child: Column(
                              children: [
                                // 头部
                                Head(name: homeCtrl.userName.value),

                                // 抬头信息
                                Info(
                                  messagesCount: NoticeCtrl.messagesCount.value,
                                  elevatorsCount: homeCtrl.elevatorsCount.value,
                                  callback: () => Get.toNamed(AppRoutes.Notice),
                                ),

                                // 快速导航
                                Navs(
                                  callback: (NavType type) =>
                                      homeCtrl.quickNavigation(type),
                                ),

                                // 最近告警
                                AlarmList(
                                  homeCtrl.listData.value,
                                  seeItem: (Map<String, dynamic> arg) =>
                                      homeCtrl.onLeave(arg),
                                ),
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                )
              : SVGLoading(),
        ),
      ),
    );
  }
}
